package com.quickcancellation.crm.utils;

import com.quickcancellation.crm.pojo.entity.Dictionary;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 反射工具类
 */
public class ReflectionUtils {

    public static List<String> getDeclaredName(Class clazz) {
        List<String> list = new ArrayList<>();

        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            list.add(field.getName());
        }

        return list;
    }

    /**
     * 获取所有成员变量的值
     * @param clazz
     */
    public static void getAllFieldValues(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                if (!field.getName().equals("id") && !field.getName().equals("serialVersionUID")){

                    String value = (String) field.get(clazz);
                    System.out.println("Field name: " + field.getName() + ", value: " + value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Dictionary dictionary = new Dictionary();
        dictionary.setId(123l);
        dictionary.setDicKey("456");
        dictionary.setDicModel("556");
        dictionary.setDicKey("789");

        getAllFieldValues(dictionary.getClass());
    }
}
